package com.xin.di.uav.common.utils;

import com.xin.di.uav.common.enums.BaseResponseCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

/**
 * 返回参数带分页
 * @author ta136817
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespJsonPageData<T> extends RespJson{
    /**
     * 返回数据
     */
    private List<T> responseData;

    /**
     * 总记录数
     */
    private long total;


    public RespJsonPageData(List<T> responseData ,long total,String responseCode ,String responseMessage){
        super(responseCode,responseMessage);
        this.responseData = responseData;
        this.total = total;
    }


    /**
     * 返回成功
     * @return
     */
    public static <T> RespJsonPageData<T> success(List<T> responseData,long total) {
        return restResult(responseData, total, BaseResponseCode.SUCCESS.getResponseCode(), BaseResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * 返回失败
     * @return
     */
    public static <T> RespJsonPageData<T> fail(List<T> responseData,long total) {
        return restResult(responseData,total,BaseResponseCode.FAIL.getResponseCode(), BaseResponseCode.FAIL.getResponseMessage());
    }

    /**
     * 更新返回对象
     * @param responseData             返回对象
     * @param total                    返回总数
     * @param responseCode             返回code码
     * @param responseMessage          返回消息
     * @param <T>
     * @return
     */
    private static <T> RespJsonPageData<T> restResult(List<T> responseData,long total,String responseCode, String responseMessage) {
        RespJsonPageData<T> respJson = new RespJsonPageData<T>();
        respJson.setResponseData(responseData);
        respJson.setTotal(total);
        respJson.setResponseCode(responseCode);
        respJson.setResponseMessage(responseMessage);
        return respJson;
    }
}
